capture log close
capture noisily log using "R:\Zentrale\ZB-S\Daten_FDSZ_Gastforscher\1_gafo\2016_0133\Figure 2.log", replace
capture noisily log using "Figure 2.log", replace

clear all
set more off
set maxvar 5000
capture noisily use "final_sample_03032023_pseudo.dta", clear
capture noisily use "R:\Zentrale\ZB-S\Daten_FDSZ_Gastforscher\1_gafo\2016_0133\data\Bundesbank Discussion Paper\final_sample_03032023.dta", clear





********************************************************************************
* diff-in-diff graph - showing statistical significance of DiD-estimator:
********************************************************************************


* Define event time
xtset id datum
order id

sort id datum
qui by id: gen temp = datum if sanctions - sanctions[_n-1] == 1
qui by id: egen temp2 = max(temp)
qui gen event_time = datum - temp2

qui by id: gen temp7 = lam100 if event_time == -12
qui by id: egen temp8 = min(temp7)
qui gen Dm12_lam100 = lam100 - temp8 

drop temp temp2 temp7 temp8 


* estimate event-time effects for a window of +/- 12/18 months
capture drop ev_ti_reg
qui gen ev_ti_reg = event_time
qui replace ev_ti_reg = . if ev_ti_reg  < -12 // pool never-sanctioned countries with sanctioned countries that are observed more than 12m before imposition of sanctions
qui replace ev_ti_reg = -13 if ev_ti_reg == . // assign never-sanctioned countries an event-time outside our window of interest
qui replace ev_ti_reg = . if ev_ti_reg >  18 // plot graph for 18m after imposition of sanctions   

* To use ev_ti_reg as factor variable in STATA, it must not be negative
qui replace ev_ti_reg = ev_ti_reg + 100 // event date is 100






*  1) Figure 2, Panel A: 
*     DE versus ABROAD - log diff, no FE
********************************************************************************

* Estimate ev_ti_reg coefficiencts:
reghdfe Dm12_lam100 b88.ev_ti_reg i(87).ev_ti_reg#c.abroad_CTRY i(89/118).ev_ti_reg#c.abroad_CTRY, noabsorb vce(cluster datum land CTRY)


* Store coefficients and confidence intervals:
capture drop lagDID cilbDID ciubDID estiDID 
qui gen lagDID  = .
qui gen cilbDID = . 
qui gen ciubDID = . 
qui gen estiDID = . 



* Calculate and store sum of coefficients and confidence intervals:
capture drop lagDE cilbDE ciubDE estiDE lagABROAD cilbABROAD ciubABROAD estiABROAD
qui gen lagDE  = .
qui gen cilbDE = . 
qui gen ciubDE = . 
qui gen estiDE = . 
qui gen lagABROAD  = .
qui gen cilbABROAD = . 
qui gen ciubABROAD = . 
qui gen estiABROAD = . 


qui replace estiABROAD = 0 in 88 // event-time=100 is baselevel defined in regression above.
qui replace lagABROAD = 88 in 88
qui replace estiDE = 0 in 88
qui replace lagDE = 88 in 88


forval i = 89(1)118 {

quietly lincom `i'.ev_ti_reg#c.abroad_CTRY + `i'.ev_ti_reg	

qui scalar pointABROAD = r(estimate)
qui scalar ci_lbABROAD = r(estimate) - invttail(e(df_r),0.025) * r(se)
qui scalar ci_ubABROAD = r(estimate) + invttail(e(df_r),0.025) * r(se)
qui display pointABROAD " " ci_lbABROAD " "ci_ubABROAD

qui replace cilbABROAD = ci_lbABROAD in `i'
qui replace ciubABROAD = ci_ubABROAD in `i'
qui replace estiABROAD = pointABROAD in `i'
qui replace lagABROAD = `i' in `i'

quietly lincom `i'.ev_ti_reg	

qui scalar pointDE = r(estimate)
qui scalar ci_lbDE = r(estimate) - invttail(e(df_r),0.025) * r(se)
qui scalar ci_ubDE = r(estimate) + invttail(e(df_r),0.025) * r(se)
qui display pointDE " " ci_lbDE " " ci_ubDE

qui replace cilbDE = ci_lbDE in `i'
qui replace ciubDE = ci_ubDE in `i'
qui replace estiDE = pointDE in `i'
qui replace lagDE = `i' in `i'

}


* Plot sum of coefficients with confidence intervals: 
capture noisily twoway /// (rarea ciubABROAD cilbABROAD lagABROAD, color(gs10)) ///
	   /// (rarea ciubDE cilbDE lagABROAD, color(gs13)) ///
	   (line estiDE lagABROAD, color(gs2) lwidth(medthick) ) ///
	   (line estiABROAD lagABROAD, color(gs2) lwidth(medthick) lpatter(longdash) yline(0, lcolor(red))) ///
	   (function y=0, range(lagABROAD) clcolor(red) ///
	   xlabel(88 "-12" 92 "-8" 96 "-4" 100 "0" 104 "4" 108 "8" 112 "12" 116 "16" ) /// 120 "20" 124 "24") ///
	   xtitle(" " "Time to sanction (months)" " ") /// ytitle("Log Assets" " ") ///
	   legend(on order(1 2) col(2) lab(1 "banks in DEU") lab(2 "banks abroad")) ///
	   name(Figure2A, replace))
graph save Figure2A.gph, replace


graph close _all








*  2) Figure 2, Panel B: 
*     DE versus ABROAD with bank-land, bank-date FE 
********************************************************************************

* Estimate ev_ti_reg coefficiencts:
reghdfe lam100 b100.ev_ti_reg i(87/99).ev_ti_reg#c.abroad_CTRY i(101/118).ev_ti_reg#c.abroad_CTRY, absorb(datum id systimeFE) vce(cluster datum land CTRY)



* Calculate and store sum of coefficients and confidence intervals:
capture drop lagDE cilbDE ciubDE estiDE lagABROAD cilbABROAD ciubABROAD estiABROAD
qui gen lagDE  = .
qui gen cilbDE = . 
qui gen ciubDE = . 
qui gen estiDE = . 
qui gen lagABROAD  = .
qui gen cilbABROAD = . 
qui gen ciubABROAD = . 
qui gen estiABROAD = . 

forval i = 88(1)99 {

quietly lincom `i'.ev_ti_reg#c.abroad_CTRY + `i'.ev_ti_reg	

qui scalar pointABROAD = r(estimate)
qui scalar ci_lbABROAD = r(estimate) - invttail(e(df_r),0.025) * r(se)
qui scalar ci_ubABROAD = r(estimate) + invttail(e(df_r),0.025) * r(se)
qui display pointABROAD " " ci_lbABROAD " "ci_ubABROAD

qui replace cilbABROAD = ci_lbABROAD in `i'
qui replace ciubABROAD = ci_ubABROAD in `i'
qui replace estiABROAD = pointABROAD in `i'
qui replace lagABROAD = `i' in `i'

quietly lincom `i'.ev_ti_reg	

qui scalar pointDE = r(estimate)
qui scalar ci_lbDE = r(estimate) - invttail(e(df_r),0.025) * r(se)
qui scalar ci_ubDE = r(estimate) + invttail(e(df_r),0.025) * r(se)
qui display pointDE " " ci_lbDE " " ci_ubDE

qui replace cilbDE = ci_lbDE in `i'
qui replace ciubDE = ci_ubDE in `i'
qui replace estiDE = pointDE in `i'
qui replace lagDE = `i' in `i'

}


qui replace estiABROAD = 0 in 100 // event-time=100 is baselevel defined in regression above.
qui replace lagABROAD = 100 in 100
qui replace estiDE = 0 in 100
qui replace lagDE = 100 in 100


forval i = 101(1)118 {

quietly lincom `i'.ev_ti_reg#c.abroad_CTRY + `i'.ev_ti_reg	

qui scalar pointABROAD = r(estimate)
qui scalar ci_lbABROAD = r(estimate) - invttail(e(df_r),0.025) * r(se)
qui scalar ci_ubABROAD = r(estimate) + invttail(e(df_r),0.025) * r(se)
qui display pointABROAD " " ci_lbABROAD " "ci_ubABROAD

qui replace cilbABROAD = ci_lbABROAD in `i'
qui replace ciubABROAD = ci_ubABROAD in `i'
qui replace estiABROAD = pointABROAD in `i'
qui replace lagABROAD = `i' in `i'

quietly lincom `i'.ev_ti_reg	

qui scalar pointDE = r(estimate)
qui scalar ci_lbDE = r(estimate) - invttail(e(df_r),0.025) * r(se)
qui scalar ci_ubDE = r(estimate) + invttail(e(df_r),0.025) * r(se)
qui display pointDE " " ci_lbDE " " ci_ubDE

qui replace cilbDE = ci_lbDE in `i'
qui replace ciubDE = ci_ubDE in `i'
qui replace estiDE = pointDE in `i'
qui replace lagDE = `i' in `i'

}


* Plot sum of coefficients with confidence intervals: 
capture noisily twoway /// (rarea ciubABROAD cilbABROAD lagABROAD, color(gs10)) ///
	   /// (rarea ciubDE cilbDE lagABROAD, color(gs13)) ///
	   (line estiDE lagABROAD, color(gs2) lwidth(medthick)) ///
	   (line estiABROAD lagABROAD, color(gs2) lwidth(medthick) lpatter(longdash) yline(0, lcolor(red))) ///
	   (function y=0, range(lagABROAD) clcolor(red) ///
	   xlabel(88 "-12" 92 "-8" 96 "-4" 100 "0" 104 "4" 108 "8" 112 "12" 116 "16" ) /// 120 "20" 124 "24") ///
	   xtitle(" " "Time to sanction (months)" " ") /// ytitle("Log Assets" " ") ///
	   legend(on order(1 2) col(2) lab(1 "banks in DEU") lab(2 "banks abroad")) ///
	   name(Figure2B, replace))
	   
graph save Figure2B.gph, replace

graph close _all





		
	




capture log close

















